package com.jerry.consumer.client;

import com.jerry.common.bean.User;
import com.jerry.common.feign.UserFeignClient;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserFallbackFactory implements FallbackFactory<UserClient> {

    @Override
    public UserClient create(Throwable throwable) {
        return new UserClient() {
            @Override
            public List<User> list() {
                List<User> userList = new ArrayList<User>();
                User user = new User();
                user.setId(-1l);
                user.setName("数据库存中没有数据--@HystrixCommand");
                userList.add(user);
                return  userList;
            }

            @Override
            public User get(Long id) {
                System.out.println("进入回退方法，异常==="+throwable);

                User user = new User();
                user.setId(-1l);
                user.setName("数据库存中没有数据--@HystrixCommand");
                return user;
            }

            @Override
            public boolean add(User user) {
                System.out.println("新增方法发生异常");
                return false;
            }

            @Override
            public boolean update(User user) {
                System.out.println("更新方法发生异常");
                return false;
            }

            @Override
            public void delete(Long id) {
                System.out.println("删除方法发生异常");
            }
        };
    }
}
